﻿using Heima.OA.Common;
using Heima.OA.IBLL;
using Heima.OA.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Heima.OA.UI.Portal.Controllers
{
    public class ActionInfoController : BaseController
    {
        public IActionInfoService ActionInfoService { get; set; }
        public IRoleInfoService RoleInfoService { get; set; }

        // GET: ActionInfo
        public string Index(PaginationQuery paginationQuery)
        {
            //var actionList = ActionInfoService.GetEntities(u => u.DelFlag == delflagNormal).AsEnumerable();
            int total = 0;
            var actionList = ActionInfoService.GetPageEntities(paginationQuery.pagesize, paginationQuery.pageindex, out total, u => u.DelFlag == delflagNormal,
                s => s.SubTime, true).AsQueryable();

            PaginationData data = new PaginationData() { total = total, rows = JsonHandler.ToJson(actionList) };
            JsonMessage msg = JsonHandler.CreateMessage((int)MsgTypeEnum.成功, "", JsonHandler.ToJson(data));

            return JsonHandler.ToJson(msg);
        }

        #region 获取所有权限数据
        public string GetList()
        {
            var actionList = ActionInfoService.GetEntities(u => u.DelFlag == delflagNormal).AsEnumerable();
            JsonMessage msg = JsonHandler.CreateMessage((int)MsgTypeEnum.成功, "", JsonHandler.ToJson(actionList));

            return JsonHandler.ToJson(msg);
        }
        #endregion

        #region 添加权限

        public string Create(ActionInfo actionInfo)
        {
            actionInfo.DelFlag = (short)Heima.OA.Model.DelFlagEnum.Normal;
            actionInfo.ModifiedOn = CDateTime.GetDefaultDateTime();
            actionInfo.SubTime = DateTime.Now;

            ActionInfoService.Add(actionInfo);
            JsonMessage message = JsonHandler.CreateMessage((int)MsgTypeEnum.成功, "添加成功!");
            return JsonHandler.ToJson(message);
        }

        #endregion
        #region 编辑
        public string Edit(ActionInfo actionInfo)
        {
            ActionInfo editActionInfo = ActionInfoService.GetModel(actionInfo.Id);
            string res = string.Empty;
            if (editActionInfo == null)
            {
                res = JsonHandler.ToJson(JsonHandler.CreateMessage((int)MsgTypeEnum.错误, "编辑错误!"));
            }
            else
            {
                editActionInfo.Url = actionInfo.Url;
                editActionInfo.HttpMethod = actionInfo.HttpMethod;
                editActionInfo.ActionName = actionInfo.ActionName;
                editActionInfo.IsMenu = actionInfo.IsMenu;
                editActionInfo.MenuIcon = actionInfo.MenuIcon;
                editActionInfo.Sort = actionInfo.Sort;
                editActionInfo.Remark = actionInfo.Remark;
                editActionInfo.ModifiedOn = DateTime.Now;
                bool success = ActionInfoService.Update(editActionInfo);
                if (success)
                {
                    res = JsonHandler.ToJson(JsonHandler.CreateMessage((int)MsgTypeEnum.成功, "编辑成功!"));
                }
            }

            return res;
        }
        #endregion
        #region 删除
        public string Delete(int id)
        {
            ActionInfo actionInfo = ActionInfoService.GetModel(id);
            string res = string.Empty;
            if (actionInfo == null)
            {
                res = JsonHandler.ToJson(JsonHandler.CreateMessage((int)MsgTypeEnum.错误, "删除错误!"));
            }
            else
            {
                actionInfo.DelFlag = (int)DelFlagEnum.Deleted;
                bool success = ActionInfoService.Update(actionInfo);
                if (success)
                {
                    res = JsonHandler.ToJson(JsonHandler.CreateMessage((int)MsgTypeEnum.成功, "删除成功!"));
                }
            }
            return res;
        }
        #endregion

        #region 设置角色
        //public ActionResult SetRole(int id)
        //{
        //    //ActionInfoService.GetEntities(u => u.Id == id).FirstOrDefault();
        //    ActionInfo actionInfo = ActionInfoService.GetModel(id);
        //    ViewBag.AllRoles = RoleInfoService.GetEntities(u => u.DelFlag == delflagNormal).ToList();
        //    return View(actionInfo);
        //}
        public string ProcessSetRole(int uid, string roleIds)
        {
            //当前用户id
            //所有打上对勾的角色
            string res = string.Empty;
            if (!string.IsNullOrEmpty(roleIds))
            {
                List<int> roleList = roleIds.Split(',').Select(p => DataConvert.GetInt(p)).ToList();
                ActionInfoService.SetRole(uid, roleList);
                JsonMessage msg = JsonHandler.CreateMessage((int)MsgTypeEnum.成功, "设置成功!");
                res = JsonHandler.ToJson(msg);
            }
            else
            {
                JsonMessage msg = JsonHandler.CreateMessage((int)MsgTypeEnum.错误, "角色为空，设置失败!");
                res = JsonHandler.ToJson(msg);
            }
            return res;
        }

        #endregion

    }
}